<?php
namespace Admin\Model;
use Think\Model;

class BpFormRightModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('RightType','number','请选择项权限类型',Model::MUST_VALIDATE)
        //array('ProjectID','number','',Model::MUST_VALIDATE,'function',Model:: MODEL_INSERT),
    );
	
	/*
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',3,'callback')
  	);
    */
	
	public function Myadd(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->add();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "添加成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "添加错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	public function Mydelet(){
		$dbName = $this->getTableName();
		$ID = I("ID");
		$sql = " UPDATE {$dbName} SET IsDel = 1 WHERE ID = {$ID}";
		$res = $this->execute($sql);
		if($res){
			$this->results['state'] = 1;
			$this->results['msg'] = "删除成功！";
			$this->results['id'] = $res;
		}else{
			$this->results['state'] = 0;
			$this->results['msg'] = "删除错误！";
		}
		return $this->results;
	}
	
	
	public function MydeletP(){
		$IDS = I("IDS");
		$ID = I("ID");
		if(!empty($IDS)){
			$where = array("ID" => array("in",$IDS));
			$this->where($where)->delete();
		}else{
			$this->delete($ID);
		}
		$this->results['state'] = 1;
		$this->results['msg'] = "删除成功！";
		$this->results['id'] = $res;
		return $this->results;
	}
	
	public function Myshow($parmArr){
		return $this->field("*")->WHERE($parmArr)->find();
	}
	
	public function MyshowList(){
		$where = "";
		$inid = "";
		$keyword = I('keyword');
        if(!empty($keyword)){
			$res = M("businessprocess")->field("ID")->where(array("Name"=>array('like','%'.$keyword.'%')))->select();
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid)){
				//$where['FormID'] = array('IN',substr($inid, 0, -1));
				$where = " BPID IN (".substr($inid, 0, -1).")";
			}
			$res = M("formcontrol")->field("ID")->where(array("CName"=>array('like','%'.$keyword.'%')))->select();
			$inid = "";
			foreach($res AS $k => $v){
				$inid .= $v['ID'].",";
			}
			if(!empty($inid) && !empty($where)){
				$where .= " OR ControlID IN (".substr($inid, 0, -1).")";
			}else{
				$where = " ControlID IN (".substr($inid, 0, -1).")";
			}
			//$where .= " OR (a.IDName LIKE '%{$keyword}%' OR a.CName LIKE '%{$keyword}%')";
		}
		$return = array();
		$count = $this->alias('a')->where($where)->count();
	    $page = $this->Mypage($count, 20);
	    $fields = " a.*, b.Name AS busineName, c.CName AS formCtrName ";
	    $join = " LEFT JOIN businessprocess AS b ON a.BPID = b.ID LEFT JOIN formcontrol AS c ON a.ControlID = c.ID";
		$res = $this->alias('a')->join($join)->where($where)->field($fields)->order("ID DESC")->limit($page->firstRow.','.$page->listRows)->select();
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	public function getBusienpList(){
		$depat = M("businessprocess");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where(array("IsDel"=>0))->select();
	}
	
	public function getFormList(){
		$depat = M("formcontrol");
		//$where['IsDel'] = 0;
		return $depat->field('ID,CName')->where()->select();
	}
	
}

?>				